﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing.Imaging;

namespace AdventureWorksWebApp
{
    /// <summary>
    /// Summary description for imageHandler
    /// </summary>
    public class imageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            int id = int.Parse(req.QueryString["ProductID"]);
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var image = (from prodphot in dc.ProductProductPhotos
                              .Where(y => y.ProductID == id)
                             from photo in dc.ProductPhotos
                             .Where(y => y.ProductPhotoID == prodphot.ProductPhotoID)
                             select photo).Single();

                byte[] b = image.LargePhoto.ToArray();
                MemoryStream ms = new MemoryStream(b);
                
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                img.Save(context.Response.OutputStream, ImageFormat.Gif);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}